//
//  CustomTableViewCell.m
//  UI10_CustomTableViewCell
//
//  Created by dllo on 16/7/27.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "CustomTableViewCell.h"
#import "NewsObject.h" // 用@class声明的类需要 在.m里去import这个类

// 各种控件的声明写在延展里
@interface CustomTableViewCell ()
@property (nonatomic, strong) UIView *customView;
@property (nonatomic, strong) UIImageView *leftImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UILabel *readLabel;

@end

@implementation CustomTableViewCell
// 不是自定义cell的初始化方法
//- (instancetype)initWithFrame:(CGRect)frame {
//    self = [super initWithFrame:frame];
//    if (self) {
//        
//    }
//    return self;
//}
// 自定义tableViewCell的初始化方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // cell的默认大小(320, 44)
        // 初始化时 cell的frame是初始值 不能为自定义布局
        // 初始化方法中只负责创建
//        NSLog(@"初始化时 cell的frame:%@", NSStringFromCGRect(self.contentView.frame));
//        _customView = [[UIView alloc] initWithFrame:CGRectZero];
//        _customView.backgroundColor = [UIColor cyanColor];
        
        // 创建出来的子视图添加在哪个父视图
        // contentView显示内容的
//        [self.contentView addSubview:_customView];

        self.leftImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:self.leftImageView];
        
        self.titleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.titleLabel];
        
        
        self.contentLabel = [[UILabel alloc] init];
        // 多行显示
        self.contentLabel.numberOfLines = 0;
        [self.contentView addSubview:self.contentLabel];
        
        self.readLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.readLabel];
        
    }
    return self;
}
// 布局子视图 及自己本身
// 负责给视图布局
- (void)layoutSubviews {
    [super layoutSubviews];
//    NSLog(@"%s,初始化时 cell的frame:%@", __FUNCTION__, NSStringFromCGRect(self.frame));
//    // 坐标要用self.contentView.frame
//    _customView.frame = CGRectMake(0, 0, self.contentView.frame.size.width, 100);
       // 其中每个数字代表的含义 让别人好理解
//    CGFloat leftMargin = 5;
//    CGFloat rightMargin = 5;
//    CGFloat topMargin = 5;
//    CGFloat bottomMargin = 5;
    self.leftImageView.frame = CGRectMake(5, 5, 100, self.contentView.frame.size.height - 10);
    self.titleLabel.frame = CGRectMake(110, 5, self.contentView.frame.size.width - 100 - 15, self.contentView.frame.size.height / 2 - 5);
    self.contentLabel.frame = CGRectMake(110, self.contentView.frame.size.height / 2, self.contentView.frame.size.width - 100 - 15, self.contentView.frame.size.height / 2 - 5);
    // label 上不要加控件 不会显示
    self.readLabel.frame = CGRectMake(self.contentView.frame.size.width -80, self.contentView.frame.size.height - 40, 60, 30);
    // 什么时候走layoutSubviews
    // 1. 当视图添加到父视图
    // 2. 当视图大小发生变化时
    // 3. 切换横竖屏
    // 4. ScrollView滚动时
}

// setter方法 在setter方法中给每一个控件赋值(用model的属性给每一个控件进行赋值)
- (void)setNewsObject:(NewsObject *)newsObject {
    _newsObject = newsObject;
    _leftImageView.image = [UIImage imageNamed:newsObject.newsImage];
        _titleLabel.text = newsObject.title;
        _contentLabel.text = newsObject.newsDescription;
        _readLabel.text = newsObject.newsNum;
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
